package com.iplay.service.cashback;

import java.util.Optional;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iplay.dao.cashback.CashbackDAO;
import com.iplay.dao.cashback.GlobalCashbackDAO;
import com.iplay.dto.cashback.CashbackStrategyCustomizationDTO;
import com.iplay.dto.cashback.CashbackStrategyDTO;
import com.iplay.entity.cashback.CashbackStrategyDO;
import com.iplay.entity.cashback.GlobalCashbackStrategyDO;
import com.iplay.vo.cashback.CashbackStrategyCustomizationVO;
import com.iplay.vo.cashback.CashbackStrategyVO;

@Service
public class CashbackServiceImpl implements CashbackService {

	@Autowired
	private GlobalCashbackDAO globalCashbackDAO;

	@Autowired
	private CashbackDAO cashbackDAO;

	private static final int GLOBAL_CASH_BACK_STRATEGY_ID = 1;

	@Override
	public boolean setDefaultCashbackStrategy(CashbackStrategyVO cashbackStrategy) {
		globalCashbackDAO.save(
				new GlobalCashbackStrategyDO(GLOBAL_CASH_BACK_STRATEGY_ID, cashbackStrategy.getPercentageForCustomer(),
						cashbackStrategy.getPercentageForRecommender(), cashbackStrategy.getPercentageForManager()));
		return true;
	}

	@Override
	public boolean customizeCashbackStrategy(CashbackStrategyCustomizationVO cashbackCustomization) {
		CashbackStrategyVO strategy = cashbackCustomization.getCashbackStrategy();
		cashbackDAO.save(new CashbackStrategyDO(cashbackCustomization.getOrderId(), strategy.getPercentageForCustomer(),
				strategy.getPercentageForRecommender(), strategy.getPercentageForManager()));
		return true;
	}

	public Optional<CashbackStrategyDO> findByOrderId(int orderId) {
		CashbackStrategyDO cashback = cashbackDAO.findOne(orderId);
		if (cashback != null)
			return Optional.of(cashback);
		GlobalCashbackStrategyDO globalCashbackDO = globalCashbackDAO.findOne(GLOBAL_CASH_BACK_STRATEGY_ID);
		return globalCashbackDO == null ? Optional.empty()
				: Optional.of(new CashbackStrategyDO(orderId, globalCashbackDO.getPercentageForCustomer(),
						globalCashbackDO.getPercentageForRecommender(), globalCashbackDO.getPercentageForManager()));
	}

	@Override
	public Optional<CashbackStrategyCustomizationDTO> findCashbackStrategyByOrderId(int orderId) {
		CashbackStrategyDTO cashbackStrategyDTO = new CashbackStrategyDTO();
		CashbackStrategyDO cashback = cashbackDAO.findOne(orderId);
		if (cashback != null) {
			BeanUtils.copyProperties(cashback, cashbackStrategyDTO);
			return Optional.of(new CashbackStrategyCustomizationDTO(orderId, cashbackStrategyDTO));
		}
		GlobalCashbackStrategyDO globalCashbackDO = globalCashbackDAO.findOne(GLOBAL_CASH_BACK_STRATEGY_ID);
		return globalCashbackDO == null ? Optional.empty()
				: Optional.of(new CashbackStrategyCustomizationDTO(orderId,
						new CashbackStrategyDTO(globalCashbackDO.getPercentageForCustomer(),
								globalCashbackDO.getPercentageForRecommender(),
								globalCashbackDO.getPercentageForManager())));
	}

	@Override
	public Optional<CashbackStrategyDTO> findGlobalCashbackStrategy() {
		GlobalCashbackStrategyDO globalCashbackDO = globalCashbackDAO.findOne(GLOBAL_CASH_BACK_STRATEGY_ID);
		return globalCashbackDO == null ? Optional.empty()
				: Optional.of(new CashbackStrategyDTO(globalCashbackDO.getPercentageForCustomer(),
						globalCashbackDO.getPercentageForRecommender(), globalCashbackDO.getPercentageForManager()));
	}
}
